/**
 * Copyright (c) 2008,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.customer.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.util.PmsUtilityComparator;
import com.ce.pms.common.vo.ChooseHouseVO;
import com.ce.pms.customer.service.IChooseSingleHouseService;
import com.ce.pms.customer.vo.ChooseSingleHouseVO;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.house.vo.FlatVO;
import com.ce.pms.house.vo.HouseGrpVO;
import com.ce.pms.house.vo.HouseSearchVO;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
/**
 * 逻辑、功能相关描述:
 * @author 易贤建  编写日期：2008-5-22
 */
public class ChooseSingleHouseAction extends PmsBaseAction {
	private IBuildingService buildingService = null;
	private IHouseService houseService = null;
	/**
	* Function:返回单个房间选择的主页面，在选择房间主页面需要显示楼栋下拉列表。
	* @paramis OwnerName说明是否需要带出客户姓名。 值为withOutOwnerName，说明不要业主姓名。值为withOwnerName时要求带出业主姓名
	* @return
	* Create author:yixianjian
	* Create on:2008-5-22
	* Edit author:
	* Edit on:
	* Why:
	 */
	public String chooseBuildingMain(@Read(key="isOwnerName" ,defaultValue="withOutOwnerName") String isOwnerName,
									 @Read(key="parentFrameId") @Out(key="parentFrameId") String parentFrameId){
		//判断是否带出业主姓名:isOwnerName=withOwnerName带，其他的不带
		if(isOwnerName != null && "withOwnerName".equals(isOwnerName))
			setAttribute("isWithOwnerName", "withOwnerName");

		List<BuildingVO> buidlingList =  buildingService.getAllBuildingList();
		setAttribute("buildings",createSelectNode(buidlingList));
		return "chooseBuildingMain";
	}
	public String initHouseList(){
		return "chooseBuildingList" ;
	}

	/**
     * Function:根据楼栋查询房屋列表。
     * @param
     * @return
     * Create author:yixianjian
     * Create on:2008-5-22
     * Edit author:易贤建
     * Edit on:
     * Why:
	 */
	public String chooseBuilding(@Read ChooseSingleHouseVO ChooseSingleHouseVO){
		if(ChooseSingleHouseVO.getBuildId() == null)
			return "chooseBuildingList" ;

		HouseSearchVO searchVO = new HouseSearchVO();
		searchVO.setBuildingId(ChooseSingleHouseVO.getBuildId());
		searchVO.setHouseGrpid(ChooseSingleHouseVO.getHouseGrpId());

		List<FlatVO> memberList = houseService.queryHouseListBy(searchVO);
		List <List> flatList = processFlatList(memberList);
		Integer storey = getStoreyHouseCountList(flatList); //获取该楼栋的层数
		if(storey == null) storey = new Integer(0);
		Integer maxHouseCount = getMaxStoreyHouseCount(flatList); //获取房号最多的楼层数
		setAttribute("storey",storey);
		setAttribute("maxHouseCount", maxHouseCount);
		setAttribute("members", flatList);
		return "chooseBuildingList" ;
	}

	/**
	 *
	* Function:获取该楼栋的层数。
	* @param 房屋列表
	* @return
	* Create author:yixianjian
	* Create on:2008-5-22
	* Edit author:
	* Edit on:
	* Why:
	 */
	public Integer getStoreyHouseCountList(List<List> memberList){
		if(memberList == null || memberList.isEmpty())
			return null;
		if(memberList.get(0) == null)
			return null;
		return  Integer.valueOf(memberList.get(0).get(0).toString());
	}
	/**
	 *
	* Function:获取楼栋层最大的房屋数.
	* @param房屋列表
	* @return
	* Create author:yixianjian
	* Create on:2008-5-22
	* Edit author:
	* Edit on:
	* Why:
	 */
	public Integer getMaxStoreyHouseCount(List<List> memberList){
		int maxCount = 0;
		if(memberList == null || memberList.isEmpty()) return maxCount;
		for(List houseItems : memberList)
			maxCount = maxCount > houseItems.size() ? maxCount : houseItems.size();
		return Integer.valueOf(maxCount - 1);
	}

	public void setChooseSingleHouseService(IChooseSingleHouseService chooseSingleHouseService) {
	}

	/**
	 *
	* Function:遍历每个flatDO,将flatDO按照每个楼层一个list方式排列。
	* @param
	* @return
	* Create author:yixianjian
	* Create on:2008-6-6
	* Edit author:
	* Edit on:
	* Why:
	 */
	private List processFlatList(List<FlatVO> flatList){
		if(flatList == null || flatList.size() == 0)
			return null;
		List<List> list = new ArrayList<List>();
		List sameFloorFlatList = null;

		Iterator<FlatVO> iterator = flatList.iterator();
		int index;
		while(iterator.hasNext()){
			FlatVO flatDO = iterator.next();
			index = isFloorExists(list,flatDO);
//			当前楼层同层的 List已经存在了，只需找到，然后添加了
			if(index != -1){
				list.get(index).add(flatDO);
			}else{
				//但前楼层map不存在，创建一个新的list，然后添加
				sameFloorFlatList = new ArrayList();
				sameFloorFlatList.add(flatDO.getFloor());
				sameFloorFlatList.add(flatDO);
				list.add(sameFloorFlatList);
			}
		}
		return list;
	}

	private int isFloorExists(List<List> list,FlatVO flatItem){
		if(list.isEmpty())
			return -1;
		List inside = list.get(list.size() - 1 );
		if(flatItem.getFloor().equals(inside.get(0))){
			return list.size() -1;
		}
		return -1;
	}
	/**
	 *
	* Function:将查询到的楼栋列表转换为页面下拉列表用的VO
	* Create author:yixianjian
	* Create on:2008-6-6
	* Edit author:
	* Edit on:
	* Why:
	 */
	private List<ChooseHouseVO> createSelectNode(List<BuildingVO> buildList){
		HouseGrpVO houseGrp = null;
		String id = null;
		String text = null;
		List<ChooseHouseVO> chooseHouseList = new ArrayList<ChooseHouseVO>();
		ChooseHouseVO temp = null;
		for(BuildingVO builditem : buildList){

			Set<HouseGrpVO> groups = builditem.getHouseGrpVos();
			Iterator<HouseGrpVO> iterator = groups.iterator();
			//不存在管理单元，先赋值再说。存在，在下面再重新设值

			if("1".equalsIgnoreCase(builditem.getHasCell())){
				while(iterator.hasNext()){
					houseGrp = iterator.next();
					temp = new ChooseHouseVO();
					id = builditem.getKeyword() + "," + houseGrp.getKeyword();
					text = builditem.getName() +"-" + houseGrp.getName() + "单元";
					temp.setBuildSeqNo(builditem.getSeqNo());
					temp.setSelectId(id);
					temp.setSelectText(text);
					chooseHouseList.add(temp);
				}
			}else {
				temp = new ChooseHouseVO(builditem.getSeqNo(),builditem.getKeyword().toString(),builditem.getName());
				chooseHouseList.add(temp);
			}

		}
		Collections.sort(chooseHouseList, PmsUtilityComparator.getComparator());
		return chooseHouseList;
	}
	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}
	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}
}
